package com.xr.system;

import com.xr.common.CommUtil;
import com.bstek.dorado.common.*;
import com.bstek.dorado.data.*;
import com.bstek.dorado.data.db.DBStatement;
import com.bstek.dorado.view.*;
import com.bstek.dorado.view.data.*;
import com.bstek.dorado.view.control.*;

/**
 * setFunctionTemplateViewModel
 */
public class setFunctionTemplateViewModel extends DefaultViewModel {
  protected void doUpdateData(ParameterSet parameters, ParameterSet outParameters)
      throws Exception {
	  String sql="";
		DBStatement dbs = new DBStatement();
		Dataset dsU = getDataset("datasetUser");
		String userId=dsU.getString("USER_ID");
      //super.doUpdateData(parameters, outParameters);
		Transaction logtransaction = TransactionManager.getTransaction();
		logtransaction.beginTransaction();
		CommUtil.dbsUpdateData(dbs, dsU, "RT_USER_FUNCTION_TEMPLATE", "USER_FUNCTION_TEMPLATE_ID", "general");
		dbs.close();
		logtransaction.commit();
		
		RecordIterator ri = dsU.recordIterator();
		ri.setVisibility(Dataset.FILTER_DELETED);
		while (ri.hasNext()) {
		  Record record = ri.nextRecord();
		  dbs.setSql("delete from RT_USER_FUNCTION_TEMPLATE where USER_FUNCTION_TEMPLATE_ID="+record.getString("USER_FUNCTION_TEMPLATE_ID"));
		  dbs.execute();
			sql="delete from rt_function_right where user_id="+userId+" and function_id in(select function_id from RT_FUNCTION_TEMPLATE_RIGHT where function_template_id="+record.getString("FUNCTION_TEMPLATE_ID")+")";
			dbs.setSql(sql);
			dbs.execute();
		}
		
		sql="insert into rt_function_right(user_id,function_id)" +
				"(select uft.user_id,ftr.function_id from RT_FUNCTION_TEMPLATE_RIGHT ftr,RT_USER_FUNCTION_TEMPLATE uft where " +
				"ftr.function_template_id=uft.function_template_id and uft.user_id="+userId+" minus " +
				"select f.user_id,function_id from rt_function_right f where f.user_id="+userId+")";
     dbs.setSql(sql);
     dbs.execute();
     dbs.close();
  }

}
